
Product
Introducing Module Reachability: Focus on the Vulnerabilities That Matter
Module Reachability filters out unreachable CVEs so you can focus on vulnerabilities that actually matter to your application.
typed-rest-client
Advanced tools
The typed-rest-client npm package is a lightweight HTTP client library for TypeScript and JavaScript. It provides a strongly-typed interface for making HTTP requests, handling responses, and managing authentication. It is particularly useful for interacting with RESTful APIs in a type-safe manner.
HTTP Requests
This feature allows you to make HTTP GET requests to a specified URL and handle the response. The example demonstrates how to create a RestClient instance and make a GET request to an API endpoint.
const { RestClient } = require('typed-rest-client/RestClient');
const client = new RestClient('my-user-agent');
async function getData() {
let res = await client.get('https://api.example.com/data');
console.log(res.result);
}
getData();
HTTP POST Requests
This feature allows you to make HTTP POST requests to a specified URL with a payload. The example demonstrates how to create a RestClient instance and make a POST request to an API endpoint with a JSON payload.
const { RestClient } = require('typed-rest-client/RestClient');
const client = new RestClient('my-user-agent');
async function postData() {
let res = await client.create('https://api.example.com/data', { key: 'value' });
console.log(res.result);
}
postData();
Authentication
This feature allows you to handle authentication using various methods such as Basic Authentication. The example demonstrates how to create a RestClient instance with Basic Authentication and make a GET request to an API endpoint.
const { RestClient } = require('typed-rest-client/RestClient');
const { BasicCredentialHandler } = require('typed-rest-client/Handlers');
const handler = new BasicCredentialHandler('username', 'password');
const client = new RestClient('my-user-agent', null, [handler]);
async function getData() {
let res = await client.get('https://api.example.com/data');
console.log(res.result);
}
getData();
Axios is a popular promise-based HTTP client for the browser and Node.js. It provides a simple and easy-to-use API for making HTTP requests and handling responses. Compared to typed-rest-client, Axios is more widely used and has a larger community, but it does not provide built-in type safety for TypeScript.
Node-fetch is a lightweight module that brings window.fetch to Node.js. It is a minimalistic library for making HTTP requests and handling responses. Compared to typed-rest-client, node-fetch is more lightweight and has a simpler API, but it lacks built-in type safety and advanced features like authentication handlers.
Superagent is a small progressive client-side HTTP request library. It is flexible and easy to use, with support for various HTTP methods and features like file uploads and authentication. Compared to typed-rest-client, Superagent is more feature-rich but does not provide built-in type safety for TypeScript.
A lightweight Rest and Http Client optimized for use with TypeScript with generics and async await.
With 0.9 just published, we believe the API surface for 1.0 has settled. More testing in progress then we will release 1.0.
import * as rm from 'typed-rest-client/RestClient';
let restc: rm.RestClient = new rm.RestClient('rest-samples',
'https://mystudentapiserver');
let res: rm.IRestResponse<Student> = await restc.get<Student>('/students/5');
console.log(res.statusCode);
console.log(res.result.name);
npm install typed-rest-client --save
See samples for complete coding examples
Typings (.d.ts) are distributed with the client, so intellisense and compile support just works from tsc
and vscode
Pre-req: prefer Node 6.9.3 LTS, minimum Node >= 4.4.7 LTS
Typings: npm install typings -g
Once (or when dependencies change):
npm install
typings install
npm run build
Run samples:
$ npm run samples
To contribute to this repository, see the contribution guide
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
FAQs
Node Rest and Http Clients for use with TypeScript
The npm package typed-rest-client receives a total of 492,962 weekly downloads. As such, typed-rest-client popularity was classified as popular.
We found that typed-rest-client demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Module Reachability filters out unreachable CVEs so you can focus on vulnerabilities that actually matter to your application.
Company News
Socket is bringing best-in-class reachability analysis into the platform — cutting false positives, accelerating triage, and cementing our place as the leader in software supply chain security.
Product
Socket is introducing a new way to organize repositories and apply repository-specific security policies.